/*----------------------------------------------
| noka select v1.0 www.97521.com                 |
| rebin 2014-01-15                             |
|---------------------------------------------*/
var nselectcustom = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; //id
    	this.name=cfg.name;//name
    	this.iwidth=cfg.width;//宽
    	this.value = cfg.value;
    	this.sdy = $(cfg.id+'_sDiv');
    	this.height = cfg.height;//高度
    	this.value = cfg.value;
    	this.readonly = ('readonly'==cfg.readonly);
    	this.disabled = ('disabled'==cfg.disabled);
    	this.text = cfg.text;
    	this.allownull = cfg.allownull;//为true表示可以为空
    	this.placeholder = cfg.placeholder;
    	this.hImg;
    	this.mImg;
    },
    show : function(){
    	this.PreLoding();
    	this.initselect();
    	this.initObjCht();
    	this.ExEvents();
    },
    //-------------------初始化下拉框-------------------
    initselect : function(){
    	var idx = 0;
        var html = [];
        var self = this;
        var bodyHtml=$(self.id+'_selectDiv');
        self.iwidth-=18;
        //------------配置初始化--------------------------
        html[idx++] = '<div id="'+self.id+'_sidiv" class="select-dropdownchecklist nk_input_lineheight nk_input_sdDiv" style="width:'+self.iwidth+'px;float:left;">';
        html[idx++] = '<input id="'+self.id+'"  name="'+self.name+'" type="hidden" value="'+self.value+'">';
        html[idx++] = '<input id="'+self.id+'_sinput" placeholder="'+self.placeholder+'" class="nk_input_lineheight nk_select_input_text" type="text" style="width:'+self.iwidth+'px;" readonly="readonly" '+(self.disabled?'disabled="disabled"':'')+' value="'+self.text+'">';
        html[idx++] = '</div>';
        if(!self.allownull){
        	html[idx++] = '<div style="float:left;color: red;" class="nk_input_imgheight">*</div>';
           	html[idx++] = '<div style="float:left;"><img style="display:none;vertical-align:middle" class="nk_input_imgheight" id="'+self.id+'_img" src="'+self.cfg.rurl+'/nokatag/formcheck/images/exclamation.gif"></img></div>';
        }
        bodyHtml.innerHTML=html.join('');
        //-----------加载事件-----------------------------
        var obj=$(self.cfg.id+'_sidiv');
        var tre=$(self.cfg.id+'_sboddiv');
        self.selectEvent(obj, tre);
    },
   //--------初始化验证-----------------
    initObjCht : function(){
    	var self = this;
    	$(self.id).veri = function(){
    	    if(($(self.id).value.trim().length<1) && !self.allownull){//不能为空
    	    	$(self.id+'_img').show();
    	    	return false;
    	    }
    	    try{$(self.id+'_img').hide();}catch(e){}
    	    return true;
    	};
    },
    //----------------外部事件---------------------
    ExEvents : function(){
    	var self = this;
    	var textObj = $(self.id+'_sinput');
    	var valueObj =$(self.id);
    	//------------设置值------------------
    	$(self.id).setValue = function(v,s){
            valueObj.value = v;
            textObj.value = s;
            try{$(self.id+'_img').hide();}catch(e){}
    	};
    	//----------------------------------
    	$(self.id).setDisabled = function(a){
    		$(self.id+'_sinput').writeAttribute({disabled:a});
    		self.disabled=a;
    	};
    	$(self.id).setReadonly = function(a){
    		$(self.id+'_sinput').writeAttribute({readonly:a});
    		self.readonly=a;
    	};
    },
    //------------------列表单击事件----------------
    ListEvent : function(json){
    	var self = this;
    	var textObj = $(self.id+'_sinput');
        var valueObj =$(self.id);
    	for(var i=0;i<json.length;i++){
    		$(self.id+'_'+i).observe('click',function(a){
    			var v=$(this.id+'_value').value;
    			var t=$(this.id+'_text').value;
    			var nod = {value:v,text:t};
    			nod.id = self.id;
    			nod.name = self.name;
    			
    			if(self.onchange(nod)){
	    			textObj.value = t;
	    			valueObj.value = v;
    			}
    			try{$(self.id+'_img').hide();}catch(e){}
            });
    		$(self.id+'_'+i).observe('mousemove',function(a){
    			this.setStyle({color:'#ffffff',backgroundColor:'#000080'});
            });
    		$(self.id+'_'+i).observe('mouseout',function(a){
    			this.setStyle({color:'#000000',backgroundColor:'#ffffff'});
            });
    	}
    },
    //===================下拉框事件============================================================
    selectEvent : function(obj,tre){
    	var self = this;
        var miFlg = true;
    	obj.observe('click',function(a){
    		if(self.readonly || self.disabled){
    			return;
    		}
    		var atop = obj.positionedOffset().top+obj.offsetHeight;
    		var aleft = obj.positionedOffset().left;
        	tre.absolutize();
        	tre.style.top=atop;
        	tre.style.left=aleft;
        	tre.setStyle({left:aleft+'px',top:atop+'px'});
        	tre.show();
        	try{
        		$(self.id+'_sboddiv').reinit();
        	}catch(e){}
        });
        obj.observe('mousemove',function(){
    		miFlg = true;
    		self.setSelectStyle(obj);
    	});
        
        obj.observe('mouseout',function(event){
        	miFlg = false;
        	var element = event.element();
        	setTimeout(function(){
                if (element.descendantOf(self.sdy) && !miFlg){
                	self.setNoSelectStyle(obj);
                	tre.hide();
                }
            }, 500);
        });
        
        tre.observe('mousemove',function(){
    		miFlg = true;
    		self.setSelectStyle(obj);
    	});
        tre.observe('mouseout',function(event){
        	miFlg = false;
        	var element = event.element();
        	setTimeout(function(){
                if (element.descendantOf(self.sdy) && !miFlg){
                	self.setNoSelectStyle(obj);
                	tre.hide();
                }
            }, 500);
        });
    },
   //----------选种状态-----------------------
    setSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#5794bf',backgroundImage:'url('+self.hImg.src+')'});
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#AFAFAF',backgroundImage:'url('+self.mImg.src+')'});
    },
    //-----------预加载图片-------------------
    PreLoding : function(){
    	var self = this;
    	self.hImg = new Image(); 
    	self.mImg = new Image();
    	self.hImg.src=self.cfg.rurl+'/nokatag/org/noka/select/dropdown_hover.png';
    	self.mImg.src=self.cfg.rurl+'/nokatag/org/noka/select/dropdown.png';
    	NBackgroundImageCache();
    }
});